#include<iostream>
using namespace std;

typedef struct node{
    int r,l;
}node;
node tree[1000001];
int N,i,ans;

void dfs(int index,int n){
    ans = ans > n? ans:n;
    if(tree[index].r) dfs(tree[index].r,n+1);
    if(tree[index].l) dfs(tree[index].l,n+1);
}

int main()
{
    scanf("%d",&N);
    for ( i = 1; i < N; i++)
    {
        scanf("%d%d",&tree[i].l,&tree[i].r);
    }
    dfs(1,1);
    if(!N) putchar('0');
    else printf("%d",ans);
    return 0;
}
